package com.shop.zookeeper.zookeeper;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

/**
 * @author Song Yinghui
 * @date 2019/12/6
 * @Version 1.0
 */
public class ZkClientTest2 {

    @Value("${zookeeper.address}")
    private String zkAddress;

    public static void listenerNode() throws InterruptedException {
        ZkClient zkClient = new ZkClient("120.27.243.49:2181");
//        String s = zkClient.create("/song", "120.27.243.49", CreateMode.PERSISTENT);
//        System.out.println(s);
        //zkClient.createPersistent("/song/serve","120.27.243.49");
        zkClient.subscribeChildChanges("/song/serve",new IZkChildListener(){

            /**
             * Called when the children of the given path changed.
             *
             * @param parentPath    The parent path
             * @param currentChilds The children or null if the root node (parent path) was deleted.
             * @throws Exception
             */
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println(parentPath);
                System.out.println(currentChilds.toString());
                System.out.println("-----");
            }
        });

        Thread.sleep(Integer.MAX_VALUE);
    }

    public static void main(String[] args) throws InterruptedException {

        //listenerNode();
    }
}
